/**
 * Project Name:JavaTest1
 * File Name:CacheUtil.java
 * Package Name:com.java.utils
 * Date:下午1:23:27
 * Copyright (c) 2021, LZ All Rights Reserved.
 *
*/

package com.jh.utils;

import java.util.concurrent.ConcurrentHashMap;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.jh.pojo.IPCamMsg;
import com.jh.pojo.RadarMsg;
import com.jh.pojo.SPInfo;

/**
 * Description: <br/>
 * Date: 下午1:23:27 <br/>
 * 
 * @author lz E-mail:withitren@outlook.com
 * @version
 * @see
 */
public class CacheUtils {
    static final Logger logger = LoggerFactory.getLogger(CacheUtils.class);

    private CacheUtils() {
    }

    private ConcurrentHashMap<Object, Object> hm = new ConcurrentHashMap<>();

    private static CacheUtils instance = null;

    /*单例*/
    public static CacheUtils getInstance() {
        if (instance == null) {
            instance = new CacheUtils();
            logger.debug("---> get new Instance!");

        }
        logger.debug("---> return Instance !" + instance.toString());
        return instance;
    }

    public void add(Object pk, Object obj) {
        hm.put(pk, obj);
        logger.debug("---> add obj!" + pk.toString());
    }

    public void update(Object pk, Object obj) {
        hm.put(pk, obj);
        logger.debug("---> update obj!" + pk.toString());
    }

    public void delete(Object pk) {
        if (IsExist(pk)) {
            hm.remove(pk);
        }
        logger.debug("---> remove obj!" + pk.toString());
    }

    public Object query(Object pk) {
        if (!IsExist(pk)) {
            return null;
        }
        logger.debug("---> query obj!" + pk.toString());
        return hm.get(pk);
    }

    public void listPrint() {
        for (Object key : hm.keySet()) {
            SPInfo spInfo = (SPInfo) hm.get((String) key);
            IPCamMsg ipCamMsg = spInfo.getIpCamMsg();
            RadarMsg radarMsg = spInfo.getRadarMsg();
            System.out.println("SPInfo Desc:" + spInfo.getSPDesc() + "  key:" + (String) key + "  ipcam value:"
                    + ipCamMsg.getIpcamIP() + "  radar value:" + radarMsg.getRadarIP());

        }
    }

    public ConcurrentHashMap<Object, Object> getCache() {
        logger.debug("---> getCache!");
        return hm;
    }

    public boolean IsExist(Object key) {
        boolean containsKey = false;
        if (key != null && key.toString().length() != 0) {
            containsKey = hm.containsKey(key);
            if (containsKey) {
                logger.debug("---> IsExist:true!");
            } else {
                logger.debug("---> IsExist:false!");
            }

        }
        return containsKey;
    }

    public Object findKeyByIPCamIP(Object ipcamIP) {
        Object rst = null;
        for (Object key : hm.keySet()) {
            SPInfo spInfo = (SPInfo) hm.get((String) key);
            IPCamMsg ipCamMsg = spInfo.getIpCamMsg();
            RadarMsg radarMsg = spInfo.getRadarMsg();
            if (ipCamMsg.getIpcamIP().equals(ipcamIP)) {

                rst = key;
                System.out.println("SPInfo Desc:" + spInfo.getSPDesc() + "  key:" + (String) key + "  ipcam value:"
                        + ipCamMsg.getIpcamIP() + "  radar value:" + radarMsg.getRadarIP());
                break;
            }

        }
        return rst;
    }

}
